package com.start.form.engine.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.common.api.Result;
import com.start.common.dataRule.DataQueryConstructor;
import com.start.common.entity.StartBaseEntity;
import com.start.core.annotations.RegisterMenu;
import com.start.core.annotations.RegisterPermission;
import com.start.form.engine.admin.StartFormAdminConstant;
import com.start.form.engine.core.StartFormEngine;
import com.start.form.engine.core.entity.StartFormConfig;
import com.start.form.engine.core.service.StartFormConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/start/formEngine/form")
@RegisterMenu(value = "表单管理", systemCode = "start:formEngine:form", component = "views/start/formEngine/form/index", path = "formEngine/index")
public class SysFormConfigController {
    @Autowired
    private StartFormConfigService startFormConfigService;

    @Autowired
    private StartFormEngine formEngine;

    @GetMapping("/page")
    public Result<?> page(Page<StartFormConfig> page, StartFormConfig entity) {
        QueryWrapper<StartFormConfig> lambdaQuery = DataQueryConstructor.createEntity(entity);
        LambdaQueryWrapper<StartFormConfig> lambda = lambdaQuery.lambda();
        lambda.orderByDesc(StartBaseEntity::getCreateTime);
        return Result.ok(startFormConfigService.page(page, lambda));
    }

    @PostMapping("/add")
    @PreAuthorize("@pas.hasPermission('start:formEngine:form:add')")
    @RegisterPermission(value = "start:formEngine:form:add", name = "添加")
    public Result<?> add(@RequestBody StartFormConfig entity, boolean synchronization) {
        if (StringUtils.isEmpty(entity.getFormCategory())) {
            entity.setFormCategory(StartFormAdminConstant.GROUP_CODE);
        }
        formEngine.createForm(entity, synchronization);
        return Result.ok(entity.getId());
    }

    @PutMapping("/updateById")
    @PreAuthorize("@pas.hasPermission('start:formEngine:form:edit')")
    @RegisterPermission(value = "start:formEngine:form:edit", name = "编辑")
    public Result<?> updateById(@RequestBody StartFormConfig entity, boolean synchronization) {
        if (StringUtils.isEmpty(entity.getFormCategory())) {
            entity.setFormCategory(StartFormAdminConstant.GROUP_CODE);
        }
        formEngine.updateForm(entity, synchronization);
        return Result.ok();
    }


    @GetMapping("/getById/{id}")
    @PreAuthorize("@pas.hasPermission('start:formEngine:form:info')")
    @RegisterPermission(value = "start:formEngine:form:info", name = "查看")
    public Result<?> getById(@PathVariable("id") String id) {
        return Result.ok(formEngine.getForm(id));
    }

    @GetMapping("/getByTableId/{id}")
    public Result<?> getByTableId(@PathVariable("id") String tableId) {
        return Result.ok(formEngine.getForm(tableId, true));
    }

    @GetMapping("/getTableOption/{tableId}")
    public Result<?> getTableOption(@PathVariable("tableId") String tableId) {
        return Result.ok(formEngine.getForm(tableId));
    }


    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('start:formEngine:form:delete')")
    @RegisterPermission(value = "start:formEngine:form:remove", name = "删除")
    public Result<?> deleteById(@PathVariable("id") String id) {
        formEngine.deleteForm(id);
        return Result.ok();
    }

    @PostMapping("/clearData/{tableId}")
    @RegisterPermission(value = "start:formEngine:form:clearData", name = "清空数据")
    public Result<?> clearData(@PathVariable("tableId") String tableId) {
        formEngine.getTableEngineService(tableId).clearData(tableId);
        return Result.ok();
    }

    @GetMapping("/createMenu/{tableId}")
    @PreAuthorize("@pas.hasPermission('start:formEngine:form:createMenu')")
    @RegisterPermission(value = "start:formEngine:form:createMenu", name = "创建菜单")
    public Result<?> createMenu(@PathVariable("tableId") String tableId, String menuLabel, String menuParentId) {
        formEngine.getTableEngineService(tableId).createMenu(tableId, menuLabel, menuParentId);
        return Result.ok();
    }

    @GetMapping("/synchronizationTable/{tableId}/{version}")
    @PreAuthorize("@pas.hasPermission('start:formEngine:form:synchronizationTable')")
    @RegisterPermission(value = "start:formEngine:form:createMenu", name = "同步或更新数据表")
    public Result<?> synchronizationTable(@PathVariable("tableId") String tableId, @PathVariable("version") Integer version) {
        formEngine.getTableEngineService(tableId).synchronizationTable(tableId);
        return Result.ok();
    }

    @GetMapping("/getAllStorage")
    public Result<?> getAllStorage() {
        return Result.ok(formEngine.getAllStorage());
    }


}
